
//
//  MKBarButtonItem+Extension.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/8/22.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    //
    /// 便利构造函数, 在扩展中只能添加便利构造函数
    convenience init(imageName: String, target: AnyObject?, action: Selector) {
        // 创建按钮
        let button = UIButton()
        button.setImage(UIImage(named: imageName), for: UIControlState.normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: UIControlState.highlighted)
        button.sizeToFit()
        
        // 设置点击事件
        button.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        // 创建UIBarButtonItem, 需要调用自己的指定构造函数
        self.init(customView: button)
    }
    
    // 
    /// 创建带按钮的UIBarButtonItem, class表示类方法, 类名.方法名()
    class func createBarButtonItem(imageName: String) -> UIBarButtonItem {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), for: UIControlState.normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: UIControlState.highlighted)
        button.sizeToFit()  // 让控件根据里面的内容是设置大小
        return UIBarButtonItem(customView: button)
    }
}
